package com.thread;

public class WaitAndNotify {
	static class WaitInnerThread1 implements Runnable{
		private Object lock;
		public WaitInnerThread1(Object lock) {
			this.lock = lock;
		}
		@Override
		public void run() {
			synchronized (lock) {
				System.out.println("thread1开始" + "\twait time" + System.currentTimeMillis());
				try {
					lock.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("thread1结束" + "\twait time" + System.currentTimeMillis());
			}
		}
	}
	
	static class WaitInnerThread2 implements Runnable{
		private Object lock;
		public WaitInnerThread2(Object lock) {
			this.lock = lock;
		}
		@Override
		public void run() {
			synchronized (lock) {
				System.out.println("thread2开始" + "\tnotify time" + System.currentTimeMillis());
				lock.notify();
				System.out.println("thread2结束" + "\tnotify time" + System.currentTimeMillis());
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		Object obj = new Object();
		WaitInnerThread1 t1 = new WaitInnerThread1(obj);
		WaitInnerThread2 t2 = new WaitInnerThread2(obj);
		Thread td1 = new Thread(t1);
		Thread td2 = new Thread(t2);
		td1.start();
		Thread.sleep(1000L);
		td2.start();
	}
}
